package com.tj.service.impl;

import com.tj.pojo.Category;
import com.tj.pojo.Dish;
import com.tj.pojo.Employee;
import com.tj.pojo.Setmeal;
import com.tj.service.CategoryService;
import com.tj.mapper.CategoryMapper;
import com.tj.service.DishService;
import com.tj.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author t
* @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
* @createDate 2023-04-25 21:48:54
*/
@Service
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public List<Category> page(int page,int pageSize) {
        return categoryMapper.getPageCategory((page-1)*pageSize,pageSize);
    }

    @Override
    public Employee getByName(Category category) {
        return categoryMapper.getByName(category);
    }


    @Override
    public int addCategory(Category category) {
        return categoryMapper.addCategory(category);
    }

    @Override
    public int deleteById(Long id) {

        //1.查询当前分类是否关联菜品，如果关联，返回-1
        Dish l = dishService.getByCategoryId(id);
        if(l!=null)  return -1;

        //2.查询当前分类是否关联套餐，如果关联，返回-1
        Setmeal ll = setmealService.getByCategoryId(id);
        if(ll!=null)  return -1;

        //3.正常删除
        return categoryMapper.deleteById(id);
    }

    @Override
    public List<Category> getAll() {
        return categoryMapper.getAll();
    }
}




